#version 330

in vec2 fragTexCoord;
in vec3 fragPosition;
in vec3 fragNormal;

uniform sampler2D texture0;
uniform vec4 colDiffuse;
uniform float intensity;
uniform vec4 direction; // 正交方向
uniform vec4 diagDirection; // 对角线方向
uniform float me;

out vec4 finalColor;

void main()
{
    vec4 texelColor = texture(texture0, fragTexCoord);
    
    // 基础颜色
    vec3 baseColor = texelColor.rgb * colDiffuse.rgb;
    
    // 如果不是墙，使用基础颜色
    if (me > 0.5) {
        finalColor = vec4(baseColor, texelColor.a);
        return;
    }

    // 如果周围没有墙，使用基础颜色
    if(direction.x < 0.5 && direction.y < 0.5 && direction.z < 0.5 && direction.w < 0.5 &&
       diagDirection.x < 0.5 && diagDirection.y < 0.5 && diagDirection.z < 0.5 && diagDirection.w < 0.5){
        finalColor = vec4(baseColor, texelColor.a);
        return;
    }

    float shadowIntensity = (intensity > 0.0) ? intensity : 0.4;
    
    // 初始化各方向阴影因子
    float leftFactor = 1.0;
    float rightFactor = 1.0;
    float upFactor = 1.0;
    float downFactor = 1.0;
    
    // 对角线阴影因子
    float upLeftFactor = 1.0;
    float upRightFactor = 1.0;
    float downLeftFactor = 1.0;
    float downRightFactor = 1.0;
    
    // 计算正交方向的阴影因子
    if(direction.x > 0.5)
        leftFactor = mix(1.0 - shadowIntensity, 1.0, smoothstep(0.0, 0.7, fragTexCoord.x));
    
    if(direction.y > 0.5)
        rightFactor = mix(1.0 - shadowIntensity, 1.0, smoothstep(0.0, 0.7, 1.0 - fragTexCoord.x));
    
    if(direction.z > 0.5)
        upFactor = mix(1.0 - shadowIntensity, 1.0, smoothstep(0.0, 0.7, fragTexCoord.y));
    
    if(direction.w > 0.5)
        downFactor = mix(1.0 - shadowIntensity, 1.0, smoothstep(0.0, 0.7, 1.0 - fragTexCoord.y));
    
    // 计算对角线方向的阴影因子
    if(diagDirection.x > 0.5)
        upLeftFactor = mix(1.0 - shadowIntensity * 0.7, 1.0, 
                          smoothstep(0.0, 1.0, length(vec2(fragTexCoord.x, fragTexCoord.y))));
    
    if(diagDirection.y > 0.5)
        upRightFactor = mix(1.0 - shadowIntensity * 0.7, 1.0, 
                           smoothstep(0.0, 1.0, length(vec2(1.0 - fragTexCoord.x, fragTexCoord.y))));
    
    if(diagDirection.z > 0.5)
        downLeftFactor = mix(1.0 - shadowIntensity * 0.7, 1.0, 
                            smoothstep(0.0, 1.0, length(vec2(fragTexCoord.x, 1.0 - fragTexCoord.y))));
    
    if(diagDirection.w > 0.5)
        downRightFactor = mix(1.0 - shadowIntensity * 0.7, 1.0, 
                             smoothstep(0.0, 1.0, length(vec2(1.0 - fragTexCoord.x, 1.0 - fragTexCoord.y))));
    
    // 组合所有阴影因子
    float shadowFactor = min(
        min(leftFactor, rightFactor),
        min(upFactor, downFactor)
    );
    
    // 应用对角线阴影
    shadowFactor = min(shadowFactor, upLeftFactor);
    shadowFactor = min(shadowFactor, upRightFactor);
    shadowFactor = min(shadowFactor, downLeftFactor);
    shadowFactor = min(shadowFactor, downRightFactor);
    
    // 确保不会太暗
    shadowFactor = max(shadowFactor, 0.3);
    
    // 应用阴影
    vec3 finalColorRGB = baseColor * shadowFactor;
    
    finalColor = vec4(finalColorRGB, texelColor.a);
}